package com.moduleapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.view.MenuItem;

import com.alibaba.android.arouter.launcher.ARouter;
import com.common.conn.OnTabReselectedListener;
import com.common.event.EventBusUtil;
import com.common.event.TabSelectedEvent;
import com.common.route.RouterPath;
import com.common.utils.FilePathManager;
import com.moduleapp.databinding.ActivityMainBinding;
import com.patch.AppUpdate;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private Map<Integer, Fragment> fragmentMap = new HashMap<>();
    private int currentTabId = R.id.tab_discovery;
    public static String IP = "http://118.178.180.228/api"; //正式
    //更新版本
    public static String versionUrl = IP + File.separator + "app" + File.separator + "latest?type=ctaa";

    @Override
    protected void onStart() {
        super.onStart();
        EventBusUtil.register(this);  // 注册事件接收器
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initBottomNavigationView();
        initFragments();
        //设置默认选中第二个
        switchFragment(currentTabId);
        binding.bottomNav.getMenu().getItem(1).setChecked(true);

        //版本更新测试
        AppUpdate.getInstance().appUpdate(0,
                this,
                "com.moduleapp",
                FilePathManager.getInstance(this).getExternalCacheDir().getPath(),
                versionUrl);
    }

    private void initBottomNavigationView() {
        binding.bottomNav.setOnItemSelectedListener(this::onNavigationItemSelected);
        binding.bottomNav.setOnItemReselectedListener(this::onNavigationItemReselected);
    }

    private void initFragments() {
        FragmentManager fragmentManager = getSupportFragmentManager();

        // 初始化并添加所有Fragment
        fragmentMap.put(R.id.tab_home, createFragment(RouterPath.HOME_FRAGMENT));
        fragmentMap.put(R.id.tab_discovery, createFragment(RouterPath.DISCOVERY_FRAGMENT));
        fragmentMap.put(R.id.tab_message, createFragment(RouterPath.MESSAGE_FRAGMENT));
        fragmentMap.put(R.id.tab_mine, createFragment(RouterPath.MINE_FRAGMENT));

        // 开启事务添加所有Fragment
        fragmentManager.beginTransaction()
                .add(R.id.fragment_container, fragmentMap.get(R.id.tab_home))
                .add(R.id.fragment_container, fragmentMap.get(R.id.tab_discovery))
                .add(R.id.fragment_container, fragmentMap.get(R.id.tab_message))
                .add(R.id.fragment_container, fragmentMap.get(R.id.tab_mine))
                .hide(fragmentMap.get(R.id.tab_home))
                .hide(fragmentMap.get(R.id.tab_message))
                .hide(fragmentMap.get(R.id.tab_mine))
                .commit();
    }

    private Fragment createFragment(String path) {
        return (Fragment) ARouter.getInstance().build(path).navigation();
    }

    private boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (currentTabId == item.getItemId()) {
            return false;
        }
        switchFragment(item.getItemId());
        return true;
    }

    private void onNavigationItemReselected(@NonNull MenuItem item) {
        Fragment fragment = fragmentMap.get(item.getItemId());
        if (fragment instanceof OnTabReselectedListener) {
            ((OnTabReselectedListener) fragment).onTabReselected();
        }
    }

    private void switchFragment(int tabId) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .hide(fragmentMap.get(currentTabId))
                .show(fragmentMap.get(tabId))
                .commit();
        currentTabId = tabId;
    }


    /**
     * @param event 1、首页 2、发现  3、消息  4、我的
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTabSelectedEvent(TabSelectedEvent event) {
        switch (event.getTabType()) {
            case 1:
                binding.bottomNav.setSelectedItemId(R.id.tab_home);
                break;
            case 2:
                binding.bottomNav.setSelectedItemId(R.id.tab_discovery);
                break;
            case 3:
                binding.bottomNav.setSelectedItemId(R.id.tab_message);
                break;
            case 4:
                binding.bottomNav.setSelectedItemId(R.id.tab_mine);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        EventBusUtil.unregister(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBusUtil.unregister(this);  // 注销事件接收器
    }
}